package com.empire.module.member.controller.admin.locationlast;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.locationlast.vo.*;
import com.empire.module.member.dal.dataobject.locationlast.LocationLastDO;
import com.empire.module.member.service.locationlast.LocationLastService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 定位信息")
@RestController
@RequestMapping("/member/location-last")
@Validated
public class LocationLastController {

    @Resource
    private LocationLastService locationLastService;

    @PostMapping("/create")
    @Operation(summary = "创建定位信息")
    @PreAuthorize("@ss.hasPermission('member:location-last:create')")
    public CommonResult<Long> createLocationLast(@Valid @RequestBody LocationLastSaveReqVO createReqVO) {
        return success(locationLastService.createLocationLast(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新定位信息")
    @PreAuthorize("@ss.hasPermission('member:location-last:update')")
    public CommonResult<Boolean> updateLocationLast(@Valid @RequestBody LocationLastSaveReqVO updateReqVO) {
        locationLastService.updateLocationLast(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除定位信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:location-last:delete')")
    public CommonResult<Boolean> deleteLocationLast(@RequestParam("id") Long id) {
        locationLastService.deleteLocationLast(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得定位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:location-last:query')")
    public CommonResult<LocationLastRespVO> getLocationLast(@RequestParam("id") Long id) {
        LocationLastDO locationLast = locationLastService.getLocationLast(id);
        return success(BeanUtils.toBean(locationLast, LocationLastRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得定位信息分页")
    @PreAuthorize("@ss.hasPermission('member:location-last:query')")
    public CommonResult<PageResult<LocationLastRespVO>> getLocationLastPage(@Valid LocationLastPageReqVO pageReqVO) {
        PageResult<LocationLastDO> pageResult = locationLastService.getLocationLastPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LocationLastRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出定位信息 Excel")
    @PreAuthorize("@ss.hasPermission('member:location-last:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLocationLastExcel(@Valid LocationLastPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LocationLastDO> list = locationLastService.getLocationLastPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "定位信息.xls", "数据", LocationLastRespVO.class,
                        BeanUtils.toBean(list, LocationLastRespVO.class));
    }

}